/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pdf;

//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerConfigurationException;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.stream.StreamResult;
//import javax.xml.transform.stream.StreamSource;
//import org.apache.avalon.framework.logger.ConsoleLogger;
//import org.apache.avalon.framework.logger.Logger;
//import org.apache.fop.apps.Driver;
//import org.xml.sax.InputSource;

/**
 *
 * @author dinhnb60088
 */
public class ConvertPDFServer extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        try {
//            String albumID = request.getParameter("albumID");
//
//            String path = getServletContext().getRealPath("/");
//            String xsl = path + "WEB-INF/albumFO.xsl";
//            String xml = path + "WEB-INF/album-" + String.valueOf(albumID) + ".xml";
//            String fo = path + "WEB-INF/headfoot.fo";
//            methodTrAX(xsl, xml, fo);
//            File file = new File(fo);
//            FileInputStream input = new FileInputStream(file);
//
//            ByteArrayOutputStream out = new ByteArrayOutputStream();
//            response.setContentType("application/pdf");
//            Driver driver = new Driver(new InputSource(input), out) {
//            };
//            Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
//            driver.setRenderer(Driver.RENDER_PDF);
//            driver.run();
//
//            byte[] content = out.toByteArray();
//            response.setContentLength(content.length);
//            response.getOutputStream().write(content);
//            response.getOutputStream().flush();

//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void methodTrAX(String xsl, String xml, String output) {
//        try {
//            TransformerFactory tf = TransformerFactory.newInstance();
//            File xslSourceFile = new File(xsl);
//            StreamSource xsltFile = new StreamSource(xslSourceFile);
//            Transformer trans = tf.newTransformer(xsltFile);
//            File xmlSourceFile = new File(xml);
//            StreamSource xmlFile = new StreamSource(xmlSourceFile);
//            StreamResult htmlFile = new StreamResult(new FileOutputStream(output));
//            trans.transform(xmlFile, htmlFile);
//        } catch (TransformerConfigurationException ex) {
//            ex.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
